Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / database / src / jvmTest / kotlin / org / meshtastic / core / database / MeshtasticDatabaseMigrationTest.kt

Displaying Raw • Download

core/database/src/jvmTest/kotlin/org/meshtastic/core/database/MeshtasticDatabaseMigrationTest.kt df62b4fd384a7d3fb867a2879aa8a14f10cd8a29 (df62b4fd) Text, 3.33 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.database

Tff7b72import T7ee787androidx.room3.testing.MigrationTestHelper
Tff7b72import T7ee787androidx.sqlite.driver.bundled.BundledSQLiteDriver
Tff7b72import T7ee787kotlinx.coroutines.test.runTest
Tff7b72import T7ee787java.io.File
Tff7b72import T7ee787kotlin.io.path.Path
Tff7b72import T7ee787kotlin.test.AfterTest
Tff7b72import T7ee787kotlin.test.Test

T8b949e/**
* Creates the earliest exported schema (v3) and walks every auto-migration up to the current version, validating the
* resulting schema against the exported JSON at each step. This is the guard that a broken migration would trip
* *before* users hit [MeshtasticDatabase]'s `fallbackToDestructiveMigration(dropAllTables = false)`, which would
* otherwise silently wipe their data.
*
* Runs on the JVM target — so it is part of `:core:database:allTests` and executes in PR CI. The JVM
* [MigrationTestHelper] reads the exported schemas straight from the module's `schemas/` directory, so no APK/asset
* packaging is needed (unlike the instrumented `androidDeviceTest` variant, which does not run in the PR pipeline).
*/
Tff7b72class T56d364MeshtasticDatabaseMigrationTest Tb4b4b4{

T8b949e// The JVM MigrationTestHelper resolves schemas relative to the Gradle test working directory (the module dir).
Tff7b72private Tff7b72val Te6edf3schemaDir Tff7b72= Te6edf3PathTb4b4b4(Ta5d6ff"Ta5d6ffschemasTa5d6ff"Tb4b4b4)

Tff7b72private Tff7b72val Te6edf3dbFile Tff7b72= Te6edf3FileTb4b4b4.Te6edf3createTempFileTb4b4b4(Ta5d6ff"Ta5d6ffmeshtastic-migrationTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ff.dbTa5d6ff"Tb4b4b4)Tb4b4b4.Te6edf3apply Tb4b4b4{ Te6edf3deleteTb4b4b4(Tb4b4b4) Tb4b4b4}

T8b949e// Not wired as a @Rule: jvmTest runs on the JUnit Platform, where JUnit4 @Rule does not apply. Connections are
T8b949e// closed inline instead.
Tff7b72private Tff7b72val Te6edf3helper Tff7b72=
Te6edf3MigrationTestHelperTb4b4b4(
Te6edf3schemaDirectoryPath Tff7b72= Te6edf3schemaDirTb4b4b4,
Te6edf3databasePath Tff7b72= Te6edf3dbFileTb4b4b4.Te6edf3toPathTb4b4b4(Tb4b4b4)Tb4b4b4,
Te6edf3driver Tff7b72= Te6edf3BundledSQLiteDriverTb4b4b4(Tb4b4b4)Tb4b4b4,
Te6edf3databaseClass Tff7b72= Te6edf3MeshtasticDatabaseTff7b72::Te6edf3classTb4b4b4,
Te6edf3databaseFactory Tff7b72= Tb4b4b4{ Te6edf3MeshtasticDatabaseConstructorTb4b4b4.Te6edf3initializeTb4b4b4(Tb4b4b4) Tb4b4b4}Tb4b4b4,
Tb4b4b4)

Tf0883e@AfterTest
Tff7b72fun Td2a8ffcleanUpTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3dbFileTb4b4b4.Te6edf3deleteTb4b4b4(Tb4b4b4)
Tb4b4b4}

Tf0883e@Test
Tff7b72fun Td2a8ffmigrateAllTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3runTest Tb4b4b4{
Te6edf3helperTb4b4b4.Te6edf3createDatabaseTb4b4b4(Te6edf3EARLIEST_SCHEMA_VERSIONTb4b4b4)Tb4b4b4.Te6edf3closeTb4b4b4(Tb4b4b4)
T8b949e// No manual migrations: every version bump is an @AutoMigration, so Room derives the full path itself.
Te6edf3helperTb4b4b4.Te6edf3runMigrationsAndValidateTb4b4b4(Te6edf3latestSchemaVersionTb4b4b4(Tb4b4b4)Tb4b4b4, Te6edf3emptyListTb4b4b4(Tb4b4b4)Tb4b4b4)Tb4b4b4.Te6edf3closeTb4b4b4(Tb4b4b4)
Tb4b4b4}

Tff7b72private Tff7b72fun Td2a8fflatestSchemaVersionTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Int Tb4b4b4{
Tff7b72val Te6edf3dbSchemas Tff7b72= Te6edf3schemaDirTb4b4b4.Te6edf3resolveTb4b4b4(Te6edf3checkNotNullTb4b4b4(Te6edf3MeshtasticDatabaseTff7b72::Te6edf3classTb4b4b4.Te6edf3qualifiedNameTb4b4b4)Tb4b4b4)Tb4b4b4.Te6edf3toFileTb4b4b4(Tb4b4b4)
Tff7b72return Te6edf3dbSchemas
Tb4b4b4.Te6edf3listFiles Tb4b4b4{ Te6edf3f Tff7b72-Tff7b72> Te6edf3fTb4b4b4.Te6edf3extension Tff7b72=Tff7b72= Ta5d6ff"Ta5d6ffjsonTa5d6ff" Tb4b4b4}
Tff7b72?.Te6edf3mapNotNull Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3nameWithoutExtensionTb4b4b4.Te6edf3toIntOrNullTb4b4b4(Tb4b4b4) Tb4b4b4}
Tff7b72?.Te6edf3maxOrNullTb4b4b4(Tb4b4b4) Tff7b72?: Te6edf3errorTb4b4b4(Ta5d6ff"Ta5d6ffNo exported Room schemas found in Tffd700$Te6edf3dbSchemasTa5d6ff"Tb4b4b4)
Tb4b4b4}

Tff7b72private Tff7b72companion Tff7b72object Tb4b4b4{
Tff7b72const Tff7b72val Te6edf3EARLIEST_SCHEMA_VERSION Tff7b72= T79c0ff3
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.04s